// ------------------------------------------------------------------------------
// 
// Copyright (c) 2007-2008 Swampware, Inc.
// 
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// 
// ------------------------------------------------------------------------------
using System;

namespace Water.Reflection.Commands
{
	/// <summary>
	/// Summary description for AssignCommand.
	/// </summary>
	public class AssignCommand : Water.Commands.Command
	{
		private Water.Commands.AssignCommand _assignCommand = new Water.Commands.AssignCommand();

		public AssignCommand()
		{
		}

		public override string Name
		{
			get { return "="; }
		}

		public override object Evaluate(Water.List expressions, Water.Environment environment, Water.Evaluator evaluator)
		{
			if(expressions.Count != 2)
			{
				throw new Water.Commands.InvalidArgumentsException(this.Name, expressions);
			}


			if (!(expressions[0] is Water.Identifier))
			{
				throw new Water.Commands.InvalidArgumentsException(this.Name, expressions);
			}
			string target = ((Water.Identifier)expressions[0]).Value;
			if(target.IndexOf(".") != -1)
			{
				object o = evaluator.Evaluate(new Water.Identifier(target.Substring(0, target.LastIndexOf("."))), environment);
				string property_name = target.Substring(target.LastIndexOf(".") + ".".Length);
				object value = evaluator.Evaluate(expressions[1], environment);


				if(o is System.Collections.IDictionary)
				{
					System.Collections.IDictionary dictionary = (System.Collections.IDictionary)o;
					dictionary[property_name] = value;
				}
				else
				{
					System.ComponentModel.PropertyDescriptor propertyDescriptor = GetProperty(o, property_name);
					if(propertyDescriptor == null)
					{
						throw new System.Exception("Property does not exist: " + property_name);
					}
					propertyDescriptor.SetValue(o, value);
				}
			}
			//TODO
//			else if(target.IndexOf("[") != -1)
//			{
//			}
			else
			{
				this._assignCommand.Evaluate(expressions, environment, evaluator);
			}


			return null;
		}

		private static System.ComponentModel.PropertyDescriptor GetProperty(object value, string name)
		{
			foreach(System.ComponentModel.PropertyDescriptor propertyDescriptor in System.ComponentModel.TypeDescriptor.GetProperties(value))
			{
				if(propertyDescriptor.Name.ToLower().Equals(name.ToLower()))
				{
					return propertyDescriptor;
				}
			}
			return null;
		}

	}
}
